package com.term.appadmin.pojo.view;

import com.alibaba.fastjson.JSONObject;
import com.term.domain.BaseDomain;
import com.term.domain.utils.DomainPage;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by chenGang on 2017/1/24.
 */
public class BaseListView<T extends BaseElementView> {
    public static Long LIMIT = 3L;

    /**
     * 元素的总个数
     */
    protected Long domainTotalCount;

    /**
     * 元素的总页数
     */
    protected Long pageCount;

    /**
     * 一页的大小，显示多少条
     */
    protected Long pageSize;

    /**
     * 第几页
     */
    protected Long pageIndex;
    /**
     * 查询对象query
     */
    protected Object searchObj;

    protected Long start;

    protected Long end;

    protected Long limit;

    /**
     * 元素集合
     */
    protected List<T> subViews=new ArrayList<>();


    public BaseListView() {
    }

    public BaseListView(DomainPage page){
        this.domainTotalCount = page.getDomainTotalCount();
        this.pageCount = page.getPageCount();
        this.pageIndex = page.getPageIndex();
        this.pageSize = page.getPageSize();

        long index = page.getPageIndex();
        long count = page.getPageCount();
        start = index - LIMIT;
        if (start <= 0L) {
            start = 1L;
        }
        end = index + LIMIT;
        if (end >= count) {
            end = count;
            if (end == 0) {
                end = start;
            }
        }
        this.limit = LIMIT;
    }
    public BaseListView(DomainPage page,Object searchObj) {
        this.domainTotalCount = page.getDomainTotalCount();
        this.pageCount = page.getPageCount();
        this.pageIndex = page.getPageIndex();
        this.pageSize = page.getPageSize();
        this.searchObj = JSONObject.toJSON(searchObj);

        long index = page.getPageIndex();
        long count = page.getPageCount();
        start = index - LIMIT;
        if (start <= 0L) {
            start = 1L;
        }
        end = index + LIMIT;
        if (end >= count) {
            end = count;
            if (end == 0) {
                end = start;
            }
        }
        this.limit = LIMIT;
    }

    public BaseListView ofQuery(Object searchObj) {
        this.searchObj = JSONObject.toJSON(searchObj);
        return this;
    }

    public BaseListView(Object query){
        this.searchObj = JSONObject.toJSON(query);
    }

    public <R extends BaseDomain> BaseListView ofDomainPage(DomainPage<R> page, Function<R, T> function) {
        this.domainTotalCount = page.getDomainTotalCount();
        this.pageCount = page.getPageCount();
        this.pageIndex = page.getPageIndex();
        this.pageSize = page.getPageSize();

        long index = page.getPageIndex();
        long count = page.getPageCount();
        start = index - LIMIT;
        if (start <= 0L) {
            start = 1L;
        }
        end = index + LIMIT;
        if (end >= count) {
            end = count;
            if (end == 0) {
                end = start;
            }
        }
        this.limit = LIMIT;

        List<R> list = page.getDomains();
        subViews = (list == null || list.isEmpty()) ? new ArrayList<>() : list.stream().map(function).collect(Collectors.toList());

        return this;
    }

    public BaseListView(Long domainTotalCount, Long pageCount,Object searchObj) {
        this.domainTotalCount = domainTotalCount;
        this.pageCount = pageCount;
        this.searchObj = JSONObject.toJSON(searchObj);
    }

    public void setSubViews(List<T> subViews) {
        this.subViews = subViews;
    }

    public Long getDomainTotalCount() {
        return domainTotalCount;
    }

    public void setDomainTotalCount(Long domainTotalCount) {
        this.domainTotalCount = domainTotalCount;
    }

    public Long getPageCount() {
        return pageCount;
    }

    public void setPageCount(Long pageCount) {
        this.pageCount = pageCount;
    }

    public List<T> getSubViews() {
        return subViews;
    }

    public Long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public Long getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Long pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Object getSearchObj() {
        return searchObj;
    }

    public void setSearchObj(Object searchObj) {
        this.searchObj = searchObj;
    }

    public Long getStart() {
        return start;
    }

    public void setStart(Long start) {
        this.start = start;
    }

    public Long getEnd() {
        return end;
    }

    public void setEnd(Long end) {
        this.end = end;
    }

    public Long getLimit() {
        return limit;
    }

    public void setLimit(Long limit) {
        this.limit = limit;
    }
}
